﻿using DevTeam;
using Splash;
using System;
using System.Data;
using System.Text.RegularExpressions;
using System.Windows.Forms;

namespace YuzTanima
{
    public partial class MainForm : Form
    {


        public MainForm()
        {
            InitializeComponent();
        }


        private void MainForm_Load(object sender, EventArgs e)
        {
            DateTime now = DateTime.Now;

            deBaslangic.DateTime = new DateTime(now.Year, now.Month, 1);
            deBitis.EditValue = DateTime.Now;
        }

        DataTable dtResult = null;
        private void btnCek_Click(object sender, EventArgs e)
        {
            try
            {
                using (FaceId Client = new FaceId(txtIP.Text, Convert.ToInt32(txtPort.Text)))
                {
                    Int32 DeviceCodePage = 65001;
                    dtResult = new DataTable();
                    dtResult.Columns.Add("Id", typeof(int));
                    dtResult.Columns.Add("Tarih", typeof(DateTime));
                    dtResult.Columns.Add("Ad", typeof(string));

                    dtResult.Clear();

                    String Answer;

                    string start = deBaslangic.DateTime.ToString("yyyy-MM-dd");
                    string end = deBitis.DateTime.AddDays(1).ToString("yyyy-MM-dd");

                    string Command = @"GetRecord(start_time=""" + start + @""" end_time=""" + end + @""")";
                    FaceId_ErrorCode ErrorCode = Client.Execute(Command, out Answer, DeviceCodePage);
                    if (ErrorCode == FaceId_ErrorCode.Success)
                    {
                        String Pattern = "\\b(time=.+)\r\n";
                        MatchCollection matches = Regex.Matches(Answer, Pattern);
                        if (matches != null)
                        {
                            foreach (Match match in matches)
                            {
                                string line = match.Groups[1].Value;
                                string[] vals = line.Split(@"""".ToCharArray());

                                string[] tarih = vals[1].Split(" ".ToCharArray())[0].Split("-".ToCharArray());
                                string[] saat = vals[1].Split(" ".ToCharArray())[1].Split(":".ToCharArray());
                                DateTime date = new DateTime(int.Parse(tarih[0]), int.Parse(tarih[1]), int.Parse(tarih[2]), int.Parse(saat[0]), int.Parse(saat[1]), int.Parse(saat[2]));

                                string id = vals[3];
                                string ad = vals[5];


                                dtResult.Rows.Add(int.Parse(id), date, ad);
                            }
                        }

                        gc.DataSource = dtResult;
                        DevExpress.XtraGrid.Columns.GridColumn col = gv.Columns["Tarih"];
                        col.DisplayFormat.FormatType = DevExpress.Utils.FormatType.DateTime;
                        col.DisplayFormat.FormatString = "dd.MM.yyyy HH:mm:ss";
                        gv.BestFitColumns();
                    }
                    else
                    {
                        MessageBox.Show("Error Code: " + ErrorCode.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void btnDB_Click(object sender, EventArgs e)
        {
            string xml = dtResult.Serialize();
            DataManager dm = new DataManager("Data Source=karbelerp.karbel.com,1430;Initial Catalog=ConfExpert;Persist Security Info=True;User ID=CEUser;Password=1034!1518");
            dm.ExecuteNonQuery("UpdFaceData", xml);
        }

        private void btnDel_Click(object sender, EventArgs e)
        {
            try
            {
                using (FaceId Client = new FaceId(txtIP.Text, Convert.ToInt32(txtPort.Text)))
                {
                    Int32 DeviceCodePage = 65001;
                    dtResult = new DataTable();

                    String Answer;

                    string start = deBaslangic.DateTime.ToString("yyyy-MM-dd");
                    string end = deBitis.DateTime.AddDays(1).ToString("yyyy-MM-dd");

                    string Command = "";

                    //TARİH ARALIĞINDA VERİ SİLMEK İÇİN;
                    //Command = @"DeleteRecord(start_time=""" + start + @""" end_time=""" + end + @""")";

                    //TÜM VERİLERİ SİLMEK İÇİN;
                    //Command = @"DeleteAllRecord()";

                    FaceId_ErrorCode ErrorCode = Client.Execute(Command, out Answer, DeviceCodePage);
                    if (ErrorCode == FaceId_ErrorCode.Success)
                    {
                        MessageBox.Show("Veriler cihazdan silindi !", "Bilgi", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        btnCek.PerformClick();
                        gc.DataSource = dtResult;
                    }
                    else
                    {
                        MessageBox.Show("Error Code: " + ErrorCode.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
    }
}
